#include "PCH.h"
#include "TestEntity.h"

namespace sol9
{
namespace entity
{

	__implementClass(TestEntity, Entity);

	TestEntity::TestEntity(core::position2df p, core::vector2df a, graphics::IImage* i)
		: position(p), velocity(0), acceleration(a), image(i), offset(0)
	{
		if (image)
			image->attach();

		offset = ((core::dimension2df)image->getDimension());
		offset /= 2.0f;
	}

	TestEntity::~TestEntity()
	{
		if (image)
			image->detach();
	}

	void TestEntity::update(u32 timeMs)
	{
		f32 delta = (f32)timeMs / 1000.0f;

		acceleration *= 0.9f;
		velocity += acceleration;
		position += velocity * delta;

		if (position.x < 0.0f + offset.x)
		{
			position.x = 0.0f + 0.1f + offset.x;
			velocity.reflection(core::vector2df(1.0f, 0.0f));
			acceleration.reflection(core::vector2df(1.0f, 0.0f));
		}

		if (position.x > 800.0f - offset.x)
		{
			position.x = 800.0f - 0.1f - offset.x;
			velocity.reflection(core::vector2df(-1.0f, 0.0f));
			acceleration.reflection(core::vector2df(-1.0f, 0.0f));
		}

		if (position.y < 0.0f + offset.y)
		{
			position.y = 0.0f + 0.1f + offset.y;
			velocity.reflection(core::vector2df(0.0f, -1.0f));
			acceleration.reflection(core::vector2df(0.0f, -1.0f));
		}

		if (position.y > 600.0f - offset.y)
		{
			position.y = 600.0f - 0.1f - offset.y;
			velocity.reflection(core::vector2df(0.0f, 1.0f));
			acceleration.reflection(core::vector2df(0.0f, 1.0f));
		}
	}

	void TestEntity::render(graphics::IPainter* painter)
	{
		if (!painter)
			return;

		painter->drawImage(position - offset, image);
	}

} // namespace entity
} // namespace sol9